#include <string.h>
#include <iostream>
using std::cout;
using std::endl;

class Student
{ 
    ~Student(){
        cout << "~Student()" << endl;
        if(_name){
            delete [] _name;
            _name = nullptr;
        }
    }

public:
    Student(int id,const char * name)
    : _id(id)
    , _name(new char[strlen(name) + 1]())
    {   
        cout << "Student(int,const char *)" << endl;
        strcpy(_name,name);
    }

  
    Student(const Student & rhs)
    : _id(rhs._id)
    , _name(new char[strlen(rhs._name) + 1]())
    {
        strcpy(_name,rhs._name);
    }

    Student & operator=(const Student & rhs){
        if(this != &rhs){
            delete [] _name;
            _name = new char[strlen(rhs._name) + 1]();
            strcpy(_name,rhs._name);
            _id = rhs._id;
        }
        return *this;
    }

    void print() const{
        cout << "id:" << _id << endl;
        cout << "name:" << _name << endl;
    }

    void * operator new(size_t sz)
	{
        cout << "operator new" << endl;
		void * ret = malloc(sz);
		return ret;
	}

	void operator delete(void * pointer)
	{
        cout << "operator delete" << endl;
		free(pointer);
	}

    //只能调用一次
    void destroy(){
        delete this;
    }
private:
    int _id;
    char * _name;
};

void test0(){
    /* Student s1(45,"Jerry"); */
    /* cout << sizeof(Student) << endl; */
    Student * stu = new Student(101,"Jack");
    stu->print();
    stu->destroy();
    /* delete stu; */
    stu = nullptr;
}

int main(void){
    test0();
    return 0;
}
